package com.hsd.znsh.controller.root;

import com.hsd.znsh.pojo.output.OpinionVO;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.pojo.query.OpinionQuery;
import com.hsd.znsh.service.UserService;
import com.hsd.znsh.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-12 20:20:02
 */
@RequestMapping("/root/opinion")
@RestController
public class RootOpinionController {

    @Autowired
    private UserService userService;

    @GetMapping("/getOpinionList")
    public ResultVO getOpinionListTotalPages(Integer size, OpinionQuery opinionQuery){
        long totalNum=userService.getOpinionListTotalPages(opinionQuery);;
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getOpinionList")
    public ResultVO getOpinionList(@PageableDefault( sort = "createDateTime",direction = Sort.Direction.DESC)Pageable pageable, OpinionQuery opinionQuery){
        List<OpinionVO> list=userService.getOpinionList(pageable,opinionQuery);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getOpinionInfo")
    public ResultVO getOpinionInfo(Integer id){
        return ResultVOUtil.success(userService.getOpinionInfo(id));
    }

    @PostMapping("/deleteOpinion")
    public ResultVO deleteOpinion(Integer id){
        userService.deletedOpinion(id);
        return ResultVOUtil.success("删除成功");
    }

}
